package com.wzy.authority.controller;

import com.alibaba.fastjson.JSONArray;
import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysPermission;
import com.wzy.authority.service.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: authorityManagement
 * @description: 权限控制器
 * @author: 1
 * @create: 2020-03-20 13:51
 **/
@Controller
@Slf4j
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private SysPermissionService sysPermissionServiceImpl;


    @ResponseBody
    @GetMapping("/menu")
    public Results<JSONArray> getLeftMenu(Integer userId) {
        log.info("接口/permission/menu的参数:" + userId);
        return sysPermissionServiceImpl.getAllMenuListByUserId(userId);
    }


    /**
     * 获取权限列表树形数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/listAllPermission")
    public Results<JSONArray> listAllPermission() {
        return sysPermissionServiceImpl.getAllMenuList();
    }


    /**
     * 根据角色id获取所有权限
     *
     * @param RoleId
     * @return
     */
    @ResponseBody
    @GetMapping("/listAllPermissionByRoleId")
    public Results<List<SysPermission>> getListPermissionById(Integer RoleId) {
        return sysPermissionServiceImpl.getAllMenuListById(RoleId);
    }

    /**
     * 获取菜单列表
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/menuAll")
    public Results<List<SysPermission>> getMenuAll() {
        return sysPermissionServiceImpl.getAllPermission();
    }


    @GetMapping("/add")
    @PreAuthorize("hasAuthority('sys:menu:add')")
    public String addView(Model model) {
        model.addAttribute("sysPermission", new SysPermission());
        return "permission/permission-add";
    }

    @PostMapping("/add")
    @ResponseBody
    public Results<Void> savePermission(@RequestBody SysPermission sysPermission) {
        log.info("接口/permission/add参数:" + sysPermission.toString());
        return sysPermissionServiceImpl.savePermission(sysPermission);
    }


    @GetMapping("/edit")
    public String editView(Model model, Integer id) {
        SysPermission sysPermission = sysPermissionServiceImpl.getPermissionById(id);
        model.addAttribute("sysPermission", sysPermission);
        return "permission/permission-add";
    }


    @PostMapping("/edit")
    @ResponseBody
    public Results<Void> editPermission(@RequestBody SysPermission sysPermission) {
        log.info("接口/permission/edit参数:" + sysPermission.toString());
        return sysPermissionServiceImpl.editPermission(sysPermission);
    }


    @PostMapping("/delete")
    @ResponseBody
    public Results<Void> deletePermission(Integer permissionId) {
        log.info("接口/permission/delete的参数:" + permissionId);
        return sysPermissionServiceImpl.deletePermission(permissionId);
    }
}
